﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class SysManager_Modules : BaseDataPage
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        this.Load += new System.EventHandler(Page_Load);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.hf_IsAdd.Value = "false";
            IniModleTree();
            this.LinkButton4.Attributes.Add("onclick", "return confirm('您确定删除吗？一旦删除将无法恢复！')");
        }
    }

    private void IniModleTree()
    {
        this.TreeView2.Nodes.Clear();
         string sql = "select * from sys_modules where parentid = 0 order by mindx";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                TreeNode node = new TreeNode();
                node.Text = "[" + dr["mindx"].ToString() + "]" + dr["modulename"].ToString();
                node.Value = dr["mid"].ToString();
                this.TreeView2.Nodes.Add(node);
                AddChildNode(node,dr["mid"].ToString());
            }
        }
        this.TreeView2.ExpandAll();
    }
    private void AddChildNode(TreeNode node,string parentid)
    {
        string sql = "select * from sys_modules where parentid = "+parentid+" order by mindx";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                TreeNode childNode = new TreeNode();
                childNode.Text = "["+dr["mindx"].ToString()+"]"+dr["modulename"].ToString();
                childNode.Value = dr["mid"].ToString();
                node.ChildNodes.Add(childNode);
                AddChildNode(childNode, dr["mid"].ToString());
            }
        }
    }
    protected void TreeView2_SelectedNodeChanged(object sender, EventArgs e)
    {
        this.hf_IsAdd.Value = "false";
        string mid = this.TreeView2.SelectedNode.Value;
        this.txtmodename.Text = this.TreeView2.SelectedNode.Text;
        string sql = @"select b.modulename as parentname,a.* from sys_modules a left 
                        join sys_modules b on b.mid = a.parentid  where a.mid = " + mid;
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            DataRow dr = ds.Tables[0].Rows[0];
            this.txtparent.Text = dr["parentname"].ToString();
            this.txtmodecode.Text = dr["modulecode"].ToString();
            this.txtmodename.Text = dr["modulename"].ToString();
            this.txtmodeorderid.Text = dr["mindx"].ToString();
            this.txtmodeurl.Text = dr["weburl"].ToString();
            this.lbmodetype2.SelectedValue = dr["moduletype"].ToString();
            this.lbMtype.SelectedValue = dr["mtype"].ToString();
            this.chkBlock.Checked = bool.Parse(dr["block"].ToString());
            this.hf_paretnid.Value = dr["parentid"].ToString();

        }
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        //保存
        string sql = "";
        if (this.hf_IsAdd.Value =="true")
        {
            sql = @"insert into sys_modules(parentid,mindx,mtype,modulecode,modulename,moduletype,weburl,creater,block)
            values(" + this.hf_paretnid.Value + ","
                   + this.txtmodeorderid.Text + ","
                   + this.lbMtype.SelectedValue + ",'"
                   + this.txtmodecode.Text + "','"
                   + this.txtmodename.Text + "',"
                   + this.lbmodetype2.SelectedValue + ",'"
                   + CheckString(this.txtmodeurl.Text) + "','"
                   + Session["UserCode"] + "'," + (this.chkBlock.Checked?1:0)
                   + ")";
        }
        else
        {
            sql = "update sys_modules set mindx=" + this.txtmodeorderid.Text + ",modulecode='" + this.txtmodecode.Text+"',mtype="+this.lbMtype.SelectedValue
                + ",modulename='" + this.txtmodename.Text + "',moduletype="+ this.lbmodetype2.SelectedValue+",modifydate='"+DateTime.Now.ToString()+"'"
                +",weburl='"+CheckString(this.txtmodeurl.Text)+"'"+ ",creater='" + Session["UserCode"] + "',block="+(this.chkBlock.Checked?1:0)+" where mid="+this.TreeView2.SelectedNode.Value;
        }
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
        {
            Response.Write("<script type='text/javascript'>alert('保存成功！')</script>");
            IniModleTree();
        }

    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        //同级
        string pName="";
        DataSet ds = mydb.DM_GetDateSet("select modulename from sys_modules where mid=" + this.hf_paretnid.Value);
        if(ds.Tables[0].Rows.Count>0){
            pName = ds.Tables[0].Rows[0]["modulename"].ToString();
        }
        DoAdd(pName);
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        //下级
        DoAdd(this.TreeView2.SelectedNode.Text);
        this.hf_paretnid.Value = this.TreeView2.SelectedValue;
    }

    private void DoAdd(string parent)
    {

        this.hf_IsAdd.Value = "true";
        this.txtparent.Text = parent;
        this.txtmodecode.Text = "";
        this.txtmodename.Text = "";
        this.txtmodeorderid.Text = "";
        this.txtmodeurl.Text = "";
        this.lbmodetype2.SelectedValue = "1";
        this.chkBlock.Checked = true;
    }
    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        //删除模块
        //this.LinkButton4.Attributes.Add("OnClick", "return confirm('您确定删除吗？一旦删除将无法恢复！');");

        if (mydb.DM_ExecuteNonQuery("delete from sys_modules where mid=" + this.TreeView2.SelectedValue) != -1)
        {
            Response.Write("<script type='text/javascript'>alert('删除成功！')</script>");
            IniModleTree();
        }
        else
        {
            Response.Write("<script type='text/javascript'>alert('删除失败！')</script>");
            IniModleTree();
        }
    }
    protected void LinkButton5_Click(object sender, EventArgs e)
    {
        //创建风格布局
        if (this.lbmodetype2.SelectedValue != "2")
        {
            ShowMessage(Response,"当前不是网格无法创建布局内容！");
            return;
        }
        DataTable dt;

        if (this.lbMtype.SelectedValue == "1")
            dt = db.DM_GetDateSet(this.txtmodeurl.Text).Tables[0];
        else
            dt = mydb.DM_GetDateSet(this.txtmodeurl.Text).Tables[0];

        string sql = "";
        string caption = "";
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            caption = mydb.DM_GetExecuteScalarObj("Exc_FieldDisPlay '" + dt.Columns[i].ColumnName + "',''").ToString();
            sql += @"if not exists(select * from sys_moduleslayout where mid = " + this.TreeView2.SelectedValue + @" and fieldname='" + dt.Columns[i].ColumnName + @"') 
                    insert into sys_moduleslayout(mid,lindx,fieldname,fieldwidth,fielddisplay,fieldvisible,gridfoot,datatype)
                    values(" + this.TreeView2.SelectedValue + "," + i.ToString() + ",'"+dt.Columns[i].ColumnName+"',80,'"+caption+"',1,0,'"+dt.Columns[i].DataType.Name.ToString().ToLower()+"')";
        }
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
        {
            ShowMessage(Response, "加载成功！");
        }
        else
        {
            ShowMessage(Response, "加载失败！");
        }
        
    }
}